home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / aa_m68k_Intel_Only / ToyViewer1.2 / Source / Background.m < prev    next >
Encoding:
Text File  |  1995-11-12  |  4.0 KB  |  194 lines

  1. /*
  2.     Background.m
  3.         based on "Background"
  4.         by Scott Hess and Andreas Windemut (1991)
  5. */
  6.  
  7. #import "Background.h"
  8. #import <dpsclient/wraps.h>
  9. #import <appkit/Application.h>
  10. #import <appkit/Window.h>
  11. #import <appkit/NXImage.h>
  12. #import <appkit/Control.h>
  13. #import <appkit/screens.h>
  14. #import <defaults/defaults.h>
  15. #import <stdio.h>
  16. #import "PSsplat.h"
  17. #import "TVController.h"
  18.  
  19. #define  PSsplat(r, p)   _PSsplat( (r)->origin.x, (r)->origin.y, \
  20.         (r)->size.width, (r)->size.height, (p)->x, (p)->y )
  21.  
  22. @implementation Background:View
  23.  
  24. static BOOL isfront = NO;
  25.  
  26. - free
  27. {
  28.     if (image != nil)
  29.         [image free];
  30.     [super free];
  31.     return self;
  32. }
  33.  
  34. - (Window *)initWinAttr
  35. {
  36.     Window *win;
  37.     NXRect rect;
  38.  
  39.     [self getFrame: &rect];
  40.     win = [[Window allocFromZone:[self zone]]
  41.             initContent:&rect style:NX_TOKENSTYLE 
  42.             backing:NX_BUFFERED  buttonMask:0 defer:NO];
  43.     [win setContentView: self];
  44.     [win useOptimizedDrawing:YES];
  45.     [win removeFromEventMask:(NX_LMOUSEDOWNMASK | NX_LMOUSEUPMASK
  46.         | NX_MOUSEMOVEDMASK | NX_LMOUSEDRAGGEDMASK
  47.         | NX_MOUSEENTEREDMASK | NX_MOUSEEXITEDMASK
  48.         | NX_KEYDOWNMASK | NX_KEYUPMASK | NX_CURSORUPDATEMASK)];
  49.     [self toBehind: self];
  50.     return win;
  51. }
  52.  
  53. - setController: sender
  54. {
  55.     controller = sender;
  56.     return self;
  57. }
  58.  
  59. - (BOOL)acceptsFirstMouse
  60. {
  61.     return YES;
  62. }
  63.  
  64. - toBehind:sender
  65. {
  66.     PSsetwindowlevel( -1, [window windowNum]);
  67.     [window orderWindow:NX_ABOVE relativeTo:0];
  68.     [window removeFromEventMask:NX_MOUSEDOWNMASK];
  69.     [controller backWinFront: (isfront = NO)];
  70.     return self;
  71. }
  72.  
  73. - toFront:sender
  74. {
  75.     PSsetwindowlevel(0, [window windowNum]);
  76.     [window orderFront:sender];
  77.     [window addToEventMask:NX_MOUSEDOWNMASK];
  78.     [controller backWinFront:(isfront = YES)];
  79.     return self;
  80. }
  81.  
  82. - mouseDown:(NXEvent *)event
  83. {
  84.     [[NXApp hide: self] unhide: self];
  85.     /* These calls are needed because of illegal action of WM. */
  86.     return [self toBehind: self];
  87. }
  88.  
  89. - (BOOL)isFront
  90. {
  91.     return isfront;
  92. }
  93.  
  94. - setImage: (NXImage *)backimage hasAlpha:(BOOL)alpha with: (int)method
  95. {
  96.     if (image != nil)
  97.         [image free];
  98.     image = [backimage copyFromZone:[self zone]];
  99.     has_alpha = alpha;
  100.     if (has_alpha)
  101.         [image setBackgroundColor: NX_COLORCLEAR];
  102.     drawMethod = method;
  103.     return self;
  104. }
  105.  
  106. /* Local Method */
  107. - paintDefault
  108. {
  109.     const NXScreen *sc;
  110.     const char *defcolor;
  111.     float bg[3];
  112.  
  113.     sc = [NXApp colorScreen];
  114.     if (sc->depth == NX_TwoBitGrayDepth
  115.      || sc->depth == NX_EightBitGrayDepth) {
  116.         defcolor = NXGetDefaultValue("NeXT1", "BWBackgroundColor");
  117.         if (!defcolor)
  118.             PSsetgray(NX_DKGRAY);    
  119.         else {
  120.             sscanf(defcolor, "%f", &bg[0]);
  121.             PSsetgray(bg[0]);    
  122.         }
  123.     }else {
  124.         defcolor = NXGetDefaultValue("NeXT1", "BackgroundColor");
  125.         if (!defcolor)
  126.             PSsetrgbcolor(0.33333, 0.33333, 0.46667);
  127.         else {
  128.             sscanf(defcolor, "%f%f%f", &bg[0], &bg[1], &bg[2]);
  129.             PSsetrgbcolor(bg[0], bg[1], bg[2]);
  130.         }
  131.     }
  132.     NXRectFill(&bounds);
  133.     return self;
  134. }
  135.  
  136. - drawSelf:(NXRect *)rect :(int)count
  137. {
  138.     int mode;
  139.     NXSize sz, screenSize;
  140.     NXPoint pnt;
  141.  
  142.     if (image == nil)
  143.         return self;
  144.     [NXApp getScreenSize:&screenSize];
  145.     [image getSize:&sz];
  146.     if (sz.width <= 0 || sz.height <= 0)
  147.         return self;
  148.  
  149.     mode = has_alpha ? NX_SOVER : NX_COPY;
  150.     if (drawMethod == 0) { /* Centering */
  151.         [self paintDefault];
  152.         pnt.x = (screenSize.width - sz.width) / 2;
  153.         pnt.y = (screenSize.height - sz.height) / 2;
  154.         [image composite:mode toPoint:&pnt];
  155.     }else {
  156.         int i, wd, ht;
  157.         NXRect irect;
  158.  
  159.         if (has_alpha)
  160.             [self paintDefault];
  161.         if (drawMethod == 1 || sz.height >= screenSize.height) {
  162.     /* Tiling */
  163.             wd = irect.size.width = sz.width;
  164.             ht = irect.size.height = sz.height;
  165.         }else {
  166.     /* Brick Work */
  167.             wd = (int)(sz.width) / 2;
  168.             pnt.x = -wd, pnt.y = sz.height;
  169.             [image composite:mode toPoint:&pnt];
  170.             pnt.x = sz.width - wd;
  171.             [image composite:mode toPoint:&pnt];
  172.             wd = irect.size.width = sz.width;
  173.             ht = irect.size.height = sz.height * 2;
  174.         }
  175.         pnt.x = pnt.y = 0;
  176.         [image composite:mode toPoint:&pnt];
  177.  
  178.         irect.origin = pnt;
  179.         for(i = wd; i < screenSize.width; i += wd) {
  180.             pnt.x = i;
  181.             PSsplat(&irect, &pnt);
  182.         }
  183.         pnt.x=0;
  184.         irect.size.width = screenSize.width;
  185.         for(i = ht; i < screenSize.height; i += ht) {
  186.             pnt.y = i;
  187.             PSsplat(&irect, &pnt);
  188.         }
  189.     }
  190.     return self;
  191. }
  192.  
  193. @end
  194.